package toDo.app.models;

import android.content.Intent;
import android.os.Parcel;
import android.os.Parcelable;

public class ToDoListItem implements Parcelable {
	public String itemName;
	public String description;
	public String comments;
	public int time;
	public String type;
	public String location;
	public int completed;
	public final int _id;
	public static final int NEW = -1;
	public static final String TYPE = "other";

	public ToDoListItem(String name, String description, String comments, int time, String type, int _id,
			String location, int completed) {
		this.itemName = sanitize(name);
		this.description = sanitize(description);
		this.comments = sanitize(comments);
		this.time = time;
		this.type = sanitize(type);
		this.location = sanitize(location);
		this._id = _id;
		this.completed = completed;
	}

	public ToDoListItem(String name, String description, String comments) {
		this(name, description, comments, NEW, TYPE, NEW, null, 0);
	}
	
	public ToDoListItem(Parcel in) {
		this(in.readString(), in.readString(), in.readString(), in.readInt(), in.readString(), in.readInt(), in
				.readString(), in.readInt());
	}

	private static String sanitize(String input) {
		return (input == null || input.matches("\\s*")) ? null : input;
	}
	
	public boolean isNew() {
		return _id == NEW;
	}

	@Override
	/*
	public String toString() {
		String display = "";
		if (itemName != null) {
			if (type != null) {
				display += " Name: " + itemName + " \tType: " + type + "\n";
			} else {
				display += " Name: " + itemName + "\n";
			}
		} else {
			if (type != null) {
				display += " Type: " + type + "\n";
			}
		}
		if (description != null) {
			display += " Description: " + description + "\n";
		}
		if (time > 0) {
			display += " Completed: " + completed + " Date: " + TimeAuthority.timeStampToComponent(time);
		} else {
			display += " Completed: " + completed;
		}
		return display;
	}
	*/
	public String toString(){
		return (itemName+"\n"+(completed==0?"Incomplete":"Complete"));
	}

	public String clickString() {
		String display = "";
		if (itemName != null) {
			if (type != null) {
				display += " Name: " + itemName + " \tType: " + type + "\n";
			} else {
				display += " Name: " + itemName + "\n";
			}
		} else {
			if (type != null) {
				display += " Type: " + type + "\n";
			}
		}
		if (description != null) {
			display += " Description: " + description + "\n";
		}
		if (comments != null) {
			display += " Comments: " + comments + "\n";
		}
		if (location != null) {
			display += " Location: " + location + "\n";
		}
		if (time > 0) {
			display += " Completed: " + completed + " Date: " + TimeAuthority.timeStampToComponent(time) + "\n";
		} else {
			display += " Completed: " + completed + "\n";
		}
		return display;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(this.itemName);
		dest.writeString(this.description);
		dest.writeString(this.comments);
		dest.writeInt(this.time);
		dest.writeString(this.type);
		dest.writeInt(this._id);
		dest.writeString(this.location);
		dest.writeInt(this.completed);
	}

	@SuppressWarnings("rawtypes")
	public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
		@Override
		public ToDoListItem createFromParcel(Parcel in) {
			return new ToDoListItem(in);
		}

		@Override
		public ToDoListItem[] newArray(int size) {
			return new ToDoListItem[size];
		}
	};

	public static Intent putItemIntoIntent(Intent intent, ToDoListItem item) {
		if (item == null)
			return intent;
		intent.putExtra(ToDoListItem.class.getName(), item);
		return intent;
	}

	public static ToDoListItem getItemFromIntent(Intent intent) {
		if (intent.hasExtra(ToDoListItem.class.getName()))
			return (ToDoListItem) intent.getParcelableExtra(ToDoListItem.class.getName());
		return null;
	}
}
